package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;


@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userInfoRepository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {

        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone, String smsUrl, String appKey) {
        //1.利用UUID创建验证码,用于编辑短信
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        StringBuilder sb = new StringBuilder(80);

        sb.append("您好,你的验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内进行验证");

        //2.发送短信
        System.out.println(sb.toString());


        //https://way.jd.com/chuangxin/VerCodesms?mobile=13568813957&content=【创信】你的验证码是：5873，3分钟内有效！&appkey=56cc5a0cf3b46bc6d0a4f407484275fe
        /*String url = "https://way.jd.com/chuangxin/VerCodesms?mobile="+phone+"&" +
                "content=【飞扬】"+sb.toString()+"&" +
                "appkey=56cc5a0cf3b46bc6d0a4f407484275fe";*/

        //spring 提供的HTTP请求发送类
        /*RestTemplate restTemplate = new RestTemplate();
        //参数1;请求url 参数2:返回值类型,参数3:请求需要的参数
        String ret = restTemplate.getForObject(smsUrl, String.class,phone,"【飞扬】"+sb.toString(),appKey);
        System.out.println(ret);
        if (!ret.contains("Success")){
            throw  new LogicException("短信发送失败");
        }*/
        //将验证码缓存到redis
        userInfoRedisService.setVerifyCode(phone, code);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判空
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        //2，验证密码
        AssertUtil.isEquals(password, rpassword, "两次输入的密码不一致");
        //3.手机号码验证
        if (this.checkPhone(phone)) {
            throw new LogicException("手机号码已经被注册");
        }
        //4. 验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码已失效或者错误");
        }
        //5. 注册
        UserInfo user = new UserInfo();
        user.setNickname(nickname);
        user.setPhone(phone);
        user.setEmail("");
        user.setPassword(password);
        user.setGender(1);
        user.setLevel(0);
        user.setCity("");
        user.setHeadImgUrl("/images/default.jpg");
        user.setInfo("");
        //
        user.setState(UserInfo.STATE_NORMAL);

        //throw new RuntimeException("sdadadadad");
        this.save(user);
    }

    @Override
    public UserInfo login(String userName, String password) {
        AssertUtil.hasLength(userName,"名字不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        UserInfo user = userInfoRepository.findByPhone(userName);

        if(user==null||!password.equals(user.getPassword())){
            throw new  LogicException("账号或者密码错误");
        }

        return user;
    }

    @Override
    public List<UserInfo> queryByCity(String city) {
        return userInfoRepository.findByCity(city);
    }
}
